package com.szxy.casdemo;

import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: zwz
 * @Date:2020/10/23 15:33
 * @Description: CAS(Compare And Swap) 并发、 原子操作
 **/
public class CasDemoTest {

    @Test
    public void test01() {
        AtomicInteger atomicInteger = new AtomicInteger(66);
        // 获取真实值，并转为相应的值
        boolean result1 = atomicInteger.compareAndSet(66, 2020);
        System.out.println("result1 = " + result1);
        boolean result2 = atomicInteger.compareAndSet(66, 2019);
        System.out.println("result2 = " + result2);
        atomicInteger.incrementAndGet();
        int result = atomicInteger.getAndSet(999);
        System.out.println("result = " + result);
        System.out.println("atomicInteger.get() = " + atomicInteger.get());
    }
}
